package com.itheima.day15.TCP.test2;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

// 发送端
public class ClientDemo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // 1.创建Socket对象
        Socket s = null;
        try {
            InetAddress byName = InetAddress.getByName("10.254.1.79");
            int port = 10002;
            s = null;
            s = new Socket(byName,port);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 2.获得字节输出流对象，开始写数据
        OutputStream os = null;
        while (true){
            System.out.println("请输入：");
            String str = sc.nextLine();
            try {
                os = s.getOutputStream();
                os.write(str.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }

            if("886".equals(str.trim())){
                System.out.println("\n---------系统提示：发送关闭---------");
                break;
            }
        }

        // 3.释放资源
        if(s != null){
            try {
                s.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(os != null){
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
